package com.padakeji.android.tumou.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.padakeji.android.tumou.R;
import com.padakeji.android.tumou.TuMouApplication;
import com.padakeji.android.tumou.util.TuMouUIHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AtanL on 2015/9/14.
 */
public class PostImageAdapter extends BaseAdapter {

    private int maxItemCount;
    private List<Integer> datas;

    public PostImageAdapter(int maxItemCount, List<Integer> datas) throws RuntimeException {
        if (!(maxItemCount > 0)) {
            throw new RuntimeException("PostImageAdapter constructor accept maxItemCount larger then 0.");
        }
        if (datas == null) {
            datas = new ArrayList<>();
        }
        this.maxItemCount = maxItemCount;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        if (datas.size() >= maxItemCount) {
            return maxItemCount;
        } else {
            return datas.size() + 1;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(TuMouApplication.getInstance()).inflate(R.layout.activity_post_item_image, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.thumbnailImage = (ImageView)convertView.findViewById(R.id.activity_post_image_thumbnail);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        int imageResId;
        if (datas.size() >= maxItemCount || position < datas.size()) {
            imageResId = datas.get(position);
            viewHolder.thumbnailImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        } else {
            imageResId = R.drawable.ic_add_black_24dp;
            viewHolder.thumbnailImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }
        viewHolder.thumbnailImage.setImageResource(imageResId);
        return convertView;
    }


    private static class ViewHolder {
        public ImageView thumbnailImage;
    }

}
